﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;


namespace TanToDriver
{
    public partial class Form1 : Form
    {
        const int FUNCODE_FIRMWARE_VERSION = 0x01;
        const int FUNCODE_FIRMWARE_DATE = 0x02;
        const int FUNCODE_FIRMWARE_CUSTOMER = 0x03;
        const int FUNCODE_ELMO_MO = 0x30;
        const int FUNCODE_ELMO_PX = 0x31;
        const int FUNCODE_ELMO_UM = 0x32;
        const int FUNCODE_ELMO_PA = 0x33;
        const int FUNCODE_ELMO_JV = 0x34;
        const int FUNCODE_ELMO_VX = 0x35;
        const int FUNCODE_ELMO_IQ = 0x36;
        const int FUNCODE_ELMO_AC = 0x37;
        const int FUNCODE_ELMO_SP = 0x38;

        public Form1()
        {
            InitializeComponent();
        }

        private void btn_OpenSerial_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == false)
            {
                serialPort1.PortName = cbx_port.Text;
                serialPort1.BaudRate = 115200;
                serialPort1.BaudRate = 115200;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;

                try
                {
                    serialPort1.Open();
                    btn_open_serial.Text = "关闭串口";
                    btn_open_serial.BackColor = Color.Yellow;
                    timer1.Enabled = true;
                    btn_update_info.Enabled = true;
                }
                catch
                {
                    MessageBox.Show("打开串口失败");
                }
            }
            else
            {
                try
                {
                    serialPort1.Close();
                    btn_open_serial.Text = "打开串口";
                    btn_open_serial.BackColor = Color.LightGreen;
                    timer1.Enabled = false;
                    btn_update_info.Enabled = false;
                }
                catch
                {
                    MessageBox.Show("关闭串口失败");
                }
            }
        }

        private void btn_update_info_Click(object sender, EventArgs e)
        {
            byte[] version_data = { 0x81, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF };
            byte[] date_data = { 0x81, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF };
            byte[] customer_data = { 0x81, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF };
            serialPort1.Write(version_data, 0, 13);
            Thread.Sleep(50);
            serialPort1.Write(date_data, 0, 13);
            Thread.Sleep(50);
            serialPort1.Write(customer_data, 0, 13);
            Thread.Sleep(50);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tbx_fmversion.Text = "v" + firmware_version.ToString();
            tbx_fmdate.Text = firmware_date_year.ToString() + "." + firmware_date_month.ToString() + "." + firmware_date_day.ToString();
            tbx_fmcustomer.Text = firmware_customer.ToString();

            listView1.Items[0].SubItems[1].Text = elmo_mo.ToString();
        }

        int firmware_version;
        int firmware_date_year, firmware_date_month, firmware_date_day;
        int firmware_customer;
        UInt64 elmo_mo;

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] read_data = new byte[13];
            int ret = 0;

            ret = serialPort1.Read(read_data, 0, 13);
            if (ret > 0)
            {
                if ((read_data[0] == 0x90) && (read_data[1] == 0x02) && (read_data[12] == 0xFF))
                {
                    if (read_data[2] == FUNCODE_FIRMWARE_VERSION)
                    {
                        firmware_version = read_data[4] * 0x100 + read_data[3];
                    }
                    else if (read_data[2] == FUNCODE_FIRMWARE_DATE)
                    {
                        firmware_date_year = read_data[6] * 0x100 + read_data[5];
                        firmware_date_month = read_data[4];
                        firmware_date_day = read_data[3];
                    }
                    else if (read_data[2] == FUNCODE_FIRMWARE_CUSTOMER)
                    {
                        firmware_customer = read_data[4] * 0x100 + read_data[3];
                    }
                    else if (read_data[2] == FUNCODE_ELMO_MO)
                    {
                        //elmo_mo = get_64_bits_var(read_data);
                        elmo_mo = read_data[3];
                    }
                }
            }
        }

        static UInt64 get_64_bits_var(byte[] data)
        {
            UInt64 ret;
            ret = (UInt64)(data[10] * 0x100000000000000 + data[9]*0x1000000000000 + data[8]*0x10000000000+ data[7] * 0x100000000 
                    + data[6] * 0x1000000 + data[5] * 0x10000 + data[5] * 0x100 + data[4]);
            return ret;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items[0].SubItems[1].Text = "hello";
        }
    }
}
